package com.android_map_app.activities;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android_map_app.R;
import com.android_map_app.data.ResultType;

public class ChangePasswordActivity extends Activity {

	private EditText mOldPw;
	private EditText mNewPw;
	private EditText mConfirmNewPw;
	
	static final int DIALOG_ID = 7; //random nr

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_password);
		
		mOldPw = (EditText) findViewById(R.id.old_password);
		mOldPw.setTypeface(Typeface.DEFAULT);
		mOldPw.setTransformationMethod(new PasswordTransformationMethod());
		mNewPw = (EditText) findViewById(R.id.new_password);
		mNewPw.setTypeface(Typeface.DEFAULT);
		mNewPw.setTransformationMethod(new PasswordTransformationMethod());
		mConfirmNewPw = (EditText) findViewById(R.id.new_password_again);
		mConfirmNewPw.setTypeface(Typeface.DEFAULT);
		mConfirmNewPw.setTransformationMethod(new PasswordTransformationMethod());
		
		Button confirmButton = (Button) findViewById(R.id.change_password_confirm);
		
		confirmButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mOldPw.getText().toString().equals("") || mNewPw.getText().toString().equals("")) {
					displayToast("Fill all fields!");
				} else if (!mNewPw.getText().toString().equals(mConfirmNewPw.getText().toString())) {
					displayToast("New passwords don't match!");
				} else {
					new SendChangeRequest().execute(mNewPw.getText().toString(),
							mOldPw.getText().toString());
				}
				
			}
		});
		
	}
	
	
	private class SendChangeRequest extends AsyncTask<String, Integer, ResultType> {
		@Override
		protected ResultType doInBackground(String... params) {
			ResultType result;
			SharedPreferences prefs = getSharedPreferences(MainActivity.USER_PREF, MODE_PRIVATE);
			try {
				URL url = new URL("http://" + getBaseContext().getString(R.string.server_name)
						+ "/enter?action=changePassword&idCode=" 
						+ prefs.getString("idCode", "")
						+ "&newPw=" + params[0]
						+ "&oldPw=" + params[1]);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(url.openStream()));
				String input = br.readLine();
				br.close();
				if (!input.equals("FAIL")) {
					SharedPreferences.Editor editor = prefs.edit();
					editor.putString("password", params[0]);
					editor.commit();
					result = ResultType.SUCCESS;
				} else {
					result = ResultType.WRONG;
				}
			} catch (Throwable e) {
				result = ResultType.ERROR;
			}
			return result;
		}
		@Override
		protected void onPreExecute() {
			showDialog(DIALOG_ID);
		}
		@Override
		protected void onPostExecute(ResultType result) {
			super.onPostExecute(result);
			dismissDialog(DIALOG_ID);
			switch (result) {
			case SUCCESS:
				displayToast("Password changed");
				Intent intent = new Intent(getApplicationContext(), TabViewActivity.class);
				intent.putExtra("tab", 2);
				startActivity(intent);
				break;
			case WRONG:
				displayToast("Username is taken");
				break;
			case ERROR:
				displayToast("Connection error.");
				break;
			}
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_ID:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage(ChangePasswordActivity.this.getString(R.string.stringChangePasswordWait));
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			return dialog;
		}
		return null;
	}
	
	private void displayToast(String message) {
		Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
		toast.show();
	}
}
